package com.book.servlet.pages;

import com.book.entity.Admin;
import com.book.service.BookService;
import com.book.service.impl.BookServiceImpl;
import com.book.utils.ThymeleafUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.java.Log;
import org.thymeleaf.context.Context;

import java.io.IOException;

@WebServlet(name = "IndexServlet", value = "/index")
@Log
public class IndexServlet extends HttpServlet {
    private BookService bookService;

    @Override
    public void init() throws ServletException {
        bookService = new BookServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Context context = new Context();
        Admin admin = (Admin) request.getSession(true).getAttribute("user");
        context.setVariable("nickname", admin.getNickname());
        context.setVariable("borrowList", bookService.getBorrowList());
        context.setVariable("borrowCount", bookService.getBorrowCount());
        context.setVariable("bookCount", bookService.getBookQuantity());
        context.setVariable("studentCount", bookService.getStudentQuantity());
        ThymeleafUtil.process("index.html", context, response.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
